package com.shuaiche.yeguan.ui.report;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shuaiche.yeguan.R;
import com.shuaiche.yeguan.util.SCEditTextPointUtils;
import com.shuaiche.yeguan.util.SCTransInformation;
import com.shuaiche.yeguan.views.qrphoto.qr.QrActivity;

import androidx.fragment.app.DialogFragment;


/**
 * Created by chen on 2019/8/7.
 */

public class SCEditVinDialogFragment extends DialogFragment implements View.OnClickListener {


    EditText etInquriyPrice;
    TextView tvSurplusNum;
    TextView tvCancel;
    TextView tvConfirm;
    LinearLayout llBtn;

    ImageView ivScanning;

    private ConfirmListener listener;
    private static final int REQUEST_FRA_SCANNING = 1006;       //VIN扫描


    public void setConfirmListener(ConfirmListener listener) {
        this.listener = listener;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.sc_dlg_edit_vin_car, container, false);
        initViews(rootView);
        return rootView;
    }

    private void initViews(View rootView) {
        etInquriyPrice = rootView.findViewById(R.id.etInquriyPrice);
        tvSurplusNum = rootView.findViewById(R.id.tv_surplus_num);
        tvCancel = rootView.findViewById(R.id.tvCancel);
        tvConfirm = rootView.findViewById(R.id.tvConfirm);
        llBtn = rootView.findViewById(R.id.llBtn);
        ivScanning = rootView.findViewById(R.id.ivScanning);

        ivScanning.setOnClickListener(this);
        tvConfirm.setOnClickListener(this);
        tvCancel.setOnClickListener(this);

        etInquriyPrice.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);

        setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialogStyleBottom);

        SCEditTextPointUtils.setStringLength(etInquriyPrice, 17, tvSurplusNum);
        etInquriyPrice.setTransformationMethod(new SCTransInformation());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode== Activity.RESULT_OK){
            if (requestCode == REQUEST_FRA_SCANNING) {
                etInquriyPrice.setText(data.getExtras().getString("result"));
            }
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvConfirm:
                listener.confirm(etInquriyPrice.getText().toString().toUpperCase());
                break;
            case R.id.tvCancel:
                listener.cancel();
                dismiss();
                break;
            case R.id.ivScanning:
                Intent intent = new Intent(getActivity(), QrActivity.class);
                startActivityForResult(intent, REQUEST_FRA_SCANNING);
                break;
        }
    }

    public interface ConfirmListener {
        void confirm(String content);

        void cancel();
    }

}
